//
//  CWWelcomeController.swift
//  CWWeibo06
//
//  Created by ycw on 2017/7/25.
//  Copyright © 2017年 xiaowei. All rights reserved.
//

import UIKit
import SnapKit
import YYWebImage

/**
 欢迎页面
 */

class CWWelcomeController: UIViewController {
    
    private lazy var backgroundImgV: UIImageView = {
        let imgV: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
        
        return imgV
        
    }()
    
    private lazy var iconImageView: UIImageView = {
       
        let imgV: UIImageView = UIImageView()
        
        imgV.layer.cornerRadius = 50
        
        imgV.layer.masksToBounds = true
        
        if let iconView = CWUserAccountViewModel.userAccountViewModel.userModel?.avatar_large {
            
            imgV.yy_setImage(with: URL(string: iconView), placeholder: UIImage(named: "avatar_default_big"))
            
        }
        
        return imgV
        
    }()
    
    fileprivate lazy var welcomeLabel: UILabel = {
       
        let label: UILabel = UILabel()
        
        if let userName = CWUserAccountViewModel.userAccountViewModel.userModel?.name {
            
            label.text = "欢迎回来,\(userName)"
        }
        
        label.textAlignment = .center
        
        label.font = UIFont.systemFont(ofSize: 16)
        
        label.textColor = UIColor.darkGray
        
        return label
        
    }()
    
    override func loadView() {
        
        view = backgroundImgV
        
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        setupUI()
        
        setupAnimation()
    
    }

    private func setupUI() {
        
        view.addSubview(iconImageView)
        
        view.addSubview(welcomeLabel)
        
        iconImageView.alpha = 0
        
        welcomeLabel.alpha = 0
        
        iconImageView.snp.makeConstraints { (make) in
            
            make.top.equalTo(view).offset(150)
            make.centerX.equalTo(view)
            make.height.width.equalTo(100)
            
        }
        
        welcomeLabel.snp.makeConstraints { (make) in
            
            make.top.equalTo(iconImageView.snp.bottom).offset(10)
            
            make.centerX.equalTo(iconImageView)
            
        }
        
    }
    
    private func setupAnimation() {
        self.iconImageView.transform = CGAffineTransform(translationX: 0, y: -50)
        UIView.animate(withDuration: 1,
                                  delay: 0,
                 usingSpringWithDamping: 0.1,
                  initialSpringVelocity: 8,
                                options: UIViewAnimationOptions.curveEaseInOut,
                             animations: {
                                self.iconImageView.alpha = 1
                                
                                self.iconImageView.transform = CGAffineTransform.identity
        }){ (_) in
            
            UIView.animate(withDuration: 1, animations: {
                self.welcomeLabel.alpha = 1
            }, completion: { (_) in
                
                //切换根视图控制器
                NotificationCenter.default.post(name: NSNotification.Name(rawValue: SwitchRootViewControllerNotification), object: nil)
                
            })
            
        }
        
    }

}
